package com.foo.cms.action;

import com.foo.cms.reqvo.TextReq;
import com.foo.cms.service.ArticleService;
import com.foo.cms.service.CategoryService;
import com.foo.cms.service.PartService;
import com.foo.cms.service.RecommendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * Created with IntelliJ IDEA.
 * User: LZS
 * Date: 12-6-24
 * Time: 下午4:43
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/text")
public class TextAction {
    private static final Logger logger = LoggerFactory.getLogger(TextAction.class);

    @Resource
    private CategoryService categoryService;

    @Resource
    private PartService partService;

    @Resource
    private RecommendService recommendService;

    @Resource
    private ArticleService articleService;

    @RequestMapping("/input")
    public ModelAndView input(){
        ModelAndView mv = new ModelAndView("text/input.jsp");
        mv.addObject("categories",categoryService.listAll());
        mv.addObject("parts",partService.listLeaf());
        mv.addObject("recommends",recommendService.listRec());
        mv.addObject("data",new TextReq());
        return mv;
    }

    @RequestMapping("/save")
    public ModelAndView save(TextReq req) throws ParseException {
        ModelAndView mv = new ModelAndView("text/input.jsp");

        try{
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
            String uid = userDetails.getUsername();
            articleService.addTextArticle(req,uid);
            mv.setViewName("redirect:/article/index.shtml?msg=OK");
        } catch (Exception e) {
            mv.addObject("categories",categoryService.listAll());
            mv.addObject("parts",partService.listLeaf());
            mv.addObject("recommends",recommendService.listRec());
            mv.addObject("data",req);
            logger.error("",e);
        }

        return mv;
    }

    @RequestMapping("/list")
    public String index(){
        return "text/list.jsp";
    }

    @RequestMapping("/edit-save")
    @ResponseBody
    public String editsave(TextReq req) throws ParseException {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String uid = userDetails.getUsername();
        articleService.editTextArticle(req,uid);
        return "编辑成功！";
    }
}
